﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
  public  class HttpHelp
    {

        
        /// <summary>
        /// 登录URL
        /// </summary>
        public string loginUrl { get; set; }

        /// <summary>
        /// 登录参数
        /// </summary>
        public string loginParameters { get; set; }

        public HttpHelp()
        { }

        public HttpHelp(string loginUrl, string loginParameters)
        {
            this.loginUrl = loginUrl;
            this.loginParameters = loginParameters;
        }

        public CookieContainer Get_Login()
        {
            CookieContainer cc = new CookieContainer();

            try
            {               
              
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(loginParameters);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginUrl);
                request.Method = "POST";    //数据提交方式
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
                //模拟一个UserAgent
                Stream newStream = request.GetRequestStream();
                newStream.Write(data, 0, data.Length);

                newStream.Close();

                request.CookieContainer = cc;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                cc.Add(response.Cookies);
                Stream stream = response.GetResponseStream();
                string WebContent = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
                if (WebContent.Contains("false"))
                {
                    throw new Exception("用户名或密码错误");
                }
            }
            catch (Exception e)
            {
                throw new Exception("登录异常：" + e.Message);
            }
            return cc;
        }

        public string GetJsonStr(string url,string parameters)
        {
            //MessageBox.Show(webBrowser1.Document.Body.ToString());

            CookieContainer cc = new CookieContainer();
            cc = Get_Login();            //获取登录Cookies          
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = System.Text.Encoding.UTF8.GetBytes(parameters);

            HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(url);
            Myrequest.Method = "POST";    //数据提交方式
            Myrequest.ContentType = "application/x-www-form-urlencoded";
            Myrequest.ContentLength = data.Length;
            Myrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";

            Myrequest.CookieContainer = cc;

            Stream Mystream = Myrequest.GetRequestStream();
            Mystream.Write(data, 0, data.Length);
            Mystream.Close();


            HttpWebResponse Myresponse = (HttpWebResponse)Myrequest.GetResponse();
            cc.Add(Myresponse.Cookies);
            Mystream = Myresponse.GetResponseStream();

            string jsonStr = new StreamReader(Mystream, System.Text.Encoding.UTF8).ReadToEnd();

            return jsonStr;
        }

      
    }


}
